/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pyin
 */
public class Command {

    private String[] patterns = {
        "(ls)\\s+((?:\\w|\\[|\\])+)$",
        "(qsa)$",
        "(lm)\\s+([-\\w\\[\\]|]+)$",
        "(sm)\\s+([-\\w\\[\\]|]+)$",
        "(qm)\\s+([-\\w\\[\\]|]+)$",
        "(help)",
        "(setup)\\s+([-\\w\\[\\]|]+)$",
        "(qs)\\s*$"
    };

    public String setup(String hostName){
        try{
            VMMachine machine = VMMachineFactory.getVMMachine(hostName);
            if(machine.setupEnv(ConfigManager.getBuildName()))
                return hostName+" is now reday for test of build "+ConfigManager.getBuildName();
            else
                return "Failed due to some unknown reason.";
        }catch(InvalidVMHostNameException e){
            return e.getMessage();
        } catch(BuildNameNotSetException e){
            return e.getMessage();
        }
    }

    public String qsa() {
        String text = "";
        List scripts = TSManager.getAllScriptNames();
        Iterator it = scripts.iterator();
        while (it.hasNext()) {
            String line = ((String) it.next()).replace(".java", "");
            text = text + line + "<br>";
        }
        return text;
    }

    public String[] getPatterns() {
        return this.patterns;
    }

    public String ls(String scriptName) {
//        System.out.println("ls " + scriptName);
        try {
            TestScript script = TSManager.getScript(scriptName);
            TSRunner.addJob(script);
            return "script " + script.getScriptName() + " has been submitted for execution";
        } catch (InvalidTSException e) {
            return e.getMessage();
        } catch (InvalidVMHostNameException e){
            return e.getMessage();
        }
    }

    public String help() {
        return getCommmandHelp();
    }

    public String getCommmandHelp() {
        return "ls(launch script): ls scriptName[machineName]buildName";
    }

    public String lm(String machineName) {
        try {
            VMMachine machine = VMMachineFactory.getVMMachine(machineName);
            if (machine == null) {
                return "The machine is not on list, please try another one";
            } else {
                return "starting up " + machineName + " ...";
            }
        } catch (InvalidVMHostNameException e) {
            return e.getMessage();
        }
    }

    public String sm(String machineName) {
        try {
            VMMachine machine = VMMachineFactory.getVMMachine(machineName);
            if (machine == null) {
                return "The machine is not on list, please try another one";
            } else {
                return "shutting down " + machineName + " ...";
            }
        } catch (InvalidVMHostNameException e) {
            return e.getMessage();
        }
    }

    public String qm(String machineName) {
        try {
            if (machineName.trim().equalsIgnoreCase("all")) {
                List machines = VMMachineFactory.getVMMachines();
                Iterator it = machines.iterator();
                String result = "";
                while (it.hasNext()) {
                    VMMachine machine = (VMMachine) it.next();
                    result = result + machine.getName() + "<br>";
                }
                return result;
            }
            VMMachine machine = VMMachineFactory.getVMMachine(machineName);
            String status = machine.isHostingMachineReady() ? "running" : "stopped";
            String[] snapshots = machine.getSnapshots();
            String ss = "";
            if (snapshots != null) {
                for (int i = 0; i < snapshots.length; i++) {

                    ss = ss + snapshots[i] + "<br>";
                }
            }
            String result = "hostname:\t" + machine.getName() + "<br>" + "host on:\t" + machine.getVmHost() + status + "<br>" + "OS:\t" + machine.getOs() + " <br>snapshots:" + ss;
            return result;
        } catch (InvalidVMHostNameException e) {
            return e.getMessage();
        }

    }
}
